<template>
<div>
  <el-card>
    <template #header>
      <span>发布公告</span>
    </template>
    <el-form label-width="auto" :model="announcementForm" :rules="rules" ref="announcementForm">
      <el-form-item label="公告标题" prop="announcementTitle">
        <el-input v-model="announcementForm.announcementTitle"></el-input>
      </el-form-item>
      <el-form-item label="公告类型" required>
        <el-select v-model="announcementForm.announcementType">
          <el-option value="维护公告" label="维护公告"/>
          <el-option value="更新公告" label="更新公告"/>
          <el-option value="紧急通知" label="紧急通知"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公告内容" required>
        <el-input type="textarea" v-model="announcementForm.content"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="Submit()">提交</el-button>
        <el-button type="primary" @click="Back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</div>
</template>
<script>
export default {
  data(){
    return{
      rules:{
        announcementTitle:[
          {required:true,message:"请输入公告标题",trigger:"blur"}
        ],
      },
      announcementForm:{
        announcementType:"",
        announcementTitle:"",
        publishMember:"admin",
        publishDate:"",
        publishTime:"",
        status:"0",
        remark:"未审批",
        content:""
      },
    }
  },
  methods:{
    // 提交公告信息
    async Submit(){
      this.announcementForm.publishDate = this.formatDate();
      this.announcementForm.publishTime = this.formatTime();
      const res = await this.$axios({
        url:"/api/addannouncement",
        method:"post",
        data:this.announcementForm,
      })
			if (res && res.data.status === 1) {
				this.$message({
					showClose: true,
					message: "添加成功",
					type: "success",
				});
        this.Back();
			} else {
				this.$message({
					showClose: true,
					message: "添加失败",
					type: "error",
				});
			}
    },
		// 处理日期函数
		formatDate() {
			const now = new Date();
			// 获取当前年份、月份（注意月份是从0开始的，所以要+1）、日期
			const year = now.getFullYear();
			const month = String(now.getMonth() + 1).padStart(2, "0");
			const date = String(now.getDate()).padStart(2, "0");
			// 拼接字符串
			return `${year}-${month}-${date}`;
		},
    // 处理时间函数
		formatTime() {
			const now = new Date();
			const hours = String(now.getHours()).padStart(2, "0"); // 小时（24小时制）
			const minutes = String(now.getMinutes()).padStart(2, "0"); // 分钟
			// 拼接字符串
			return `${hours}:${minutes}`;
		},
    Back(){
      this.$router.go(-1);
    },
  },
  mounted() {
  }
}
</script>
<style scoped>

</style>